using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.DataContainers.SqlServer;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_UserprofileEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Userprofile userprofile;

        if (Int32.TryParse(Request["id"], out id))
            userprofile = Userprofile.Load(id);
        else
            userprofile = null;

        if (userprofile != null)
        {
            tbxSiteuserId.Text = userprofile.SiteuserId.ToString();
			if (!string.IsNullOrEmpty(userprofile.FirstName))
				tbxFirstName.Text = userprofile.FirstName;
			if (!string.IsNullOrEmpty(userprofile.LastName))
				tbxLastName.Text = userprofile.LastName;
			if (userprofile.DateOfBirth != SqlDateTime.MinValue)
				tbxDateOfBirth.Text = userprofile.DateOfBirth.ToShortDateString();
			if (!string.IsNullOrEmpty(userprofile.Country))
				tbxCountry.Text = userprofile.Country;
			if (!string.IsNullOrEmpty(userprofile.PostalCode))
				tbxPostalCode.Text = userprofile.PostalCode;
			if (!string.IsNullOrEmpty(userprofile.PhoneNumber))
				tbxPhoneNumber.Text = userprofile.PhoneNumber;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        int siteuserId = Security.BindIntegerInput(tbxSiteuserId.Text, _securityLevel);
		string firstName = Security.BindStringInput(tbxFirstName.Text, _securityLevel);
		string lastName = Security.BindStringInput(tbxLastName.Text, _securityLevel);
		DateTime dateOfBirth = Security.BindDateTimeInput(tbxDateOfBirth.Text, _securityLevel);
		string country = Security.BindStringInput(tbxCountry.Text, _securityLevel);
		string postalCode = Security.BindStringInput(tbxPostalCode.Text, _securityLevel);
		string phoneNumber = Security.BindStringInput(tbxPhoneNumber.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Userprofile.Save(
				id, 
				siteuserId,
				firstName,
				lastName,
				dateOfBirth,
				country,
				postalCode,
				phoneNumber
                ))
                    Response.Redirect("userprofilelist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Userprofile userprofile = Userprofile.Create(
				siteuserId,
				firstName,
				lastName,
				dateOfBirth,
				country,
				postalCode,
				phoneNumber
                );

            if (userprofile != null)
                Response.Redirect("userprofilelist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("userprofilelist.aspx");
    }
}
